/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actionForms;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import tchat.Message;
import tchat.Personne;

/**
 *
 * @author Hippolyte
 */
public class CalculForm extends org.apache.struts.action.ActionForm implements Serializable{

    private String username;
    
    private int idMessage;
    
    public ArrayList<Personne> personnes = new ArrayList<Personne>();
    public ArrayList<Message> messages = new ArrayList<Message>();

    
    public ArrayList<Personne> personnesConnectees() {
        ArrayList<Personne> connectees = new ArrayList<Personne>();

        for (Personne p : personnes) {
            if (p.isConnecte()) {
                connectees.add(p);
            }
        }

        return connectees;
    }
    
    public Personne getPersonne(String pseudo){
        
        if(pseudo == null) {
            return null;
        }
        
        System.out.println("getPersonne " + pseudo);
        System.out.println(this);
        for (Personne p : personnes) {
            System.out.println(p.getNom());
            if (p.getNom() != null && p.getNom().equalsIgnoreCase(pseudo)) {
                return p;
            }
        }
        addPersonne(pseudo);
        return getPersonne(pseudo);
    }
    
    public void addPersonne(String nom){
        System.out.println(this);
        System.out.println("add personne " + nom);
        personnes.add(new Personne(nom, true));
    }
    
//    public boolean addMessage(){
//        return addMessage("toto","test");
//    }
//    public boolean addMessage(String message){
//        System.out.println("pour " + username + " ==> " + getPersonne(username) + " : " + message);
//        Message mess = new Message(idMessage++, message, getPersonne(username));
//        messages.add(mess);
//        System.out.println("a ajoute " + mess);
//        return true;
//    }

    public boolean addMessage(String personne, String message) {
        if (personne == null || message == null || message.isEmpty()) {
            return false;
        }
        
        System.out.println("pour " + personne + " ==> " + getPersonne(personne) + " : " + message);
//        messages.add(new Message(idMessage++, message, getPersonne(personne)));
        Message mess = new Message(idMessage++, message, getPersonne(personne));
        messages.add(mess);
        System.out.println("a ajoute " + mess);
        
        sauvChat();
        
        return true;
    }

    public String afficheMessages() {
//        System.out.println("affiche message pour " + this);
        String s = "";
        for (Message m : messages) {
            s += m.toString() + "\n";
        }
//        System.out.println("s = " + s);
        return s;
    }
    
    
    public String afficheConnectes() {
//        System.out.println("affiche connectes pour " + this);
        String s = "";
        for (Personne p : personnesConnectees()) {
            s += p.getNom() + "\n";
        }
        if (personnesConnectees().size() > 0){
            s = s.substring(0, s.lastIndexOf("\n"));
        }
//        System.out.println("s = " + s);
        return s;
    }
    
    public void deconnecte(String personne) {
        Personne p = getPersonne(personne);
        personnes.remove(p);
        p.setConnecte(false);
        personnes.add(p);
    }
    
    /**
     *
     */
    public CalculForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        
        return errors;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }
    
    public boolean initDeserialisation() {
        
        System.out.println("deserialise " + System.getProperty("user.dir") 
                + "\\chat.ser ...");

        try {
            FileInputStream fichier = new FileInputStream("chat.ser");
            ObjectInputStream ois = new ObjectInputStream(fichier);
            CalculForm deser = (CalculForm) ois.readObject();
            personnes.addAll(deser.personnes);
            messages.addAll(deser.messages);
            System.out.println("... " + deser.messages.size() 
                + " messages & " + deser.personnes.size() + " personnes");
        } catch (java.io.IOException e) {
            System.out.println("IOException");
            System.out.println(e);
            return false;
        } catch (ClassNotFoundException e) {
            System.out.println(e);
            System.out.println("ClassNotFoundException");
            return false;
        }


        return true;
    }

    public boolean sauvChat() {
        
        System.out.println("serialise " + System.getProperty("user.dir") 
                + "\\chat.ser : " + messages.size() 
                + " messages & " + personnes.size() + " personnes");
        
        try {
            FileOutputStream fichier = new FileOutputStream("chat.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fichier);
            oos.writeObject(this);
            oos.flush();
            oos.close();
        } catch (java.io.IOException e) {
            System.out.println(e);
            System.out.println("cannot serialize tchat");
            return false;
        }
        return true;
    }
}
